package GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import modelo.Facturas;
import persistencia.MetodosFacturas;


/**
 * 
 * @author Antonio David
 */
public class JPBorrarFacturas extends javax.swing.JPanel {
    //JPanel jpmc;
    JPBorrarFacturas jpmm;
    ArrayList<Facturas> fa;
    Integer nFactura;
    
    
    public void actualizarComboFacturas() throws SQLException, ClassNotFoundException{
        jComboFactura.removeAllItems();
        MetodosFacturas mtf= new MetodosFacturas();
        fa = mtf.getFacturas();
        for (int i = 0; i < fa.size(); i++) {
            jComboFactura.addItem(fa.get(i).getId_facturas()); //+fa.get(i).getNombre());
        }
    }
    
    /**
     * Creates new form JPMarcaCreacion
     */
    public JPBorrarFacturas() throws SQLException, ClassNotFoundException {
        initComponents();
        actualizarComboFacturas();

        jbBorrarFactura.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int idFactura = Integer.parseInt((String) jComboFactura.getSelectedItem());
                Facturas nFactura = new Facturas(Integer.toString(idFactura));
                MetodosFacturas mtf = null;
               
                try {
                    mtf = new MetodosFacturas();
                    mtf.delFacturas(nFactura);
                    System.out.println ("Factura borrada");
                    
                    actualizarComboFacturas();
                    JOptionPane.showMessageDialog(null, "Factura Borrada");
                } catch (ClassNotFoundException ex){
                    System.err.println ("DRIVER NO ENCONTRADO.");
                    ex.printStackTrace();
                } catch (SQLException ex) {
                    System.err.println ("SQLEXCEPTION.");
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(null, "Error al Borrar Factura");
                } 
            }
        });
        
    }

    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jlNombreFactura = new javax.swing.JLabel();
        jComboFactura = new javax.swing.JComboBox();
        jbBorrarFactura = new javax.swing.JButton();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jlNombreFactura.setText("Factura a Borrar");
        add(jlNombreFactura, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 130, 100, 20));

        jComboFactura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboFacturaActionPerformed(evt);
            }
        });
        add(jComboFactura, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 130, 278, -1));

        jbBorrarFactura.setText("Borrar");
        add(jbBorrarFactura, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 190, 160, 60));
    }// </editor-fold>//GEN-END:initComponents

    private void jComboFacturaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboFacturaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboFacturaActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JComboBox jComboFactura;
    private javax.swing.JButton jbBorrarFactura;
    private javax.swing.JLabel jlNombreFactura;
    // End of variables declaration//GEN-END:variables
}
